package cn.schoolwow.flow.monitor.module.business.testcase.flow;

import cn.schoolwow.ams.domain.block.common.operation.CustomerMessage;
import cn.schoolwow.ams.domain.block.common.operation.ListBlockHandlerBlockOperation;
import cn.schoolwow.ams.domain.block.list.common.ListBlockOperationHandler;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockOption;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.BlockOperationPosition;
import cn.schoolwow.ams.module.block.listBlock.common.domain.operation.ListBlockRequest;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;


public class AddTestCaseButtonsFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        addResetButton(flowContext);
        addExecuteButton(flowContext);
        addExecuteFlowByCategoryName(flowContext);
        addExecuteAllFlow(flowContext);
    }

    @Override
    public String name() {
        return "添加测试用例按钮列表";
    }

    private void addResetButton(FlowContext flowContext){
        EntityTableListBlockOption entityTableListBlockOption = flowContext.checkInstanceData(EntityTableListBlockOption.class);
        FlowMonitorOption flowMonitorOption = flowContext.checkInstanceData(FlowMonitorOption.class);

        ListBlockHandlerBlockOperation listBlockHandlerBlockOperation = new ListBlockHandlerBlockOperation("重置测试流程", new ListBlockOperationHandler() {
            @Override
            public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                flowMonitorOption.flowMonitorQuickFlow.startFlow(new ResetTestCaseFlow())
                        .execute();
                return null;
            }
        });
        listBlockHandlerBlockOperation.position = BlockOperationPosition.RightTop;
        entityTableListBlockOption.blockOperationList.add(listBlockHandlerBlockOperation);
    }

    private void addExecuteButton(FlowContext flowContext){
        EntityTableListBlockOption entityTableListBlockOption = flowContext.checkInstanceData(EntityTableListBlockOption.class);
        FlowMonitorOption flowMonitorOption = flowContext.checkInstanceData(FlowMonitorOption.class);

        ListBlockOperationHandler listBlockOperationHandler = new ListBlockOperationHandler() {
            @Override
            public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                CustomerMessage customerMessage = flowMonitorOption.flowMonitorQuickFlow.startFlow(new ExecuteTestCaseFlow())
                        .putInstanceData(listBlockRequest)
                        .execute()
                        .checkInstanceData(CustomerMessage.class);
                return customerMessage;
            }
        };
        {
            ListBlockHandlerBlockOperation listBlockHandlerBlockOperation = new ListBlockHandlerBlockOperation("执行流程", listBlockOperationHandler);
            listBlockHandlerBlockOperation.position = BlockOperationPosition.Row;
            listBlockHandlerBlockOperation.showCustomerMessage = true;
            entityTableListBlockOption.blockOperationList.add(listBlockHandlerBlockOperation);
        }
        {
            ListBlockHandlerBlockOperation listBlockHandlerBlockOperation = new ListBlockHandlerBlockOperation("批量执行流程", listBlockOperationHandler);
            listBlockHandlerBlockOperation.position = BlockOperationPosition.MultipleSelect;
            listBlockHandlerBlockOperation.showCustomerMessage = true;
            entityTableListBlockOption.blockOperationList.add(listBlockHandlerBlockOperation);
        }
    }

    private void addExecuteFlowByCategoryName(FlowContext flowContext){
        EntityTableListBlockOption entityTableListBlockOption = flowContext.checkInstanceData(EntityTableListBlockOption.class);
        FlowMonitorOption flowMonitorOption = flowContext.checkInstanceData(FlowMonitorOption.class);

        ListBlockHandlerBlockOperation listBlockHandlerBlockOperation = new ListBlockHandlerBlockOperation("执行分类流程", new ListBlockOperationHandler() {
            @Override
            public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                CustomerMessage customerMessage = flowMonitorOption.flowMonitorQuickFlow.startFlow(new ExecuteTestCaseFlow())
                        .putInstanceData(listBlockRequest)
                        .execute()
                        .checkInstanceData(CustomerMessage.class);
                return customerMessage;
            }
        });
        listBlockHandlerBlockOperation.position = BlockOperationPosition.RightTop;
        listBlockHandlerBlockOperation.showCustomerMessage = true;
        entityTableListBlockOption.blockOperationList.add(listBlockHandlerBlockOperation);
    }

    private void addExecuteAllFlow(FlowContext flowContext){
        EntityTableListBlockOption entityTableListBlockOption = flowContext.checkInstanceData(EntityTableListBlockOption.class);
        FlowMonitorOption flowMonitorOption = flowContext.checkInstanceData(FlowMonitorOption.class);

        ListBlockHandlerBlockOperation listBlockHandlerBlockOperation = new ListBlockHandlerBlockOperation("执行全部测试用例", new ListBlockOperationHandler() {
            @Override
            public CustomerMessage handle(ListBlockRequest listBlockRequest) throws Exception {
                CustomerMessage customerMessage = flowMonitorOption.flowMonitorQuickFlow.startFlow(new ExecuteTestCaseFlow())
                        .putInstanceData(listBlockRequest)
                        .execute()
                        .checkInstanceData(CustomerMessage.class);
                return customerMessage;
            }
        });
        listBlockHandlerBlockOperation.position = BlockOperationPosition.RightTop;
        listBlockHandlerBlockOperation.showCustomerMessage = true;
        entityTableListBlockOption.blockOperationList.add(listBlockHandlerBlockOperation);
    }
}
